package thread;

public class ThreadMethods {

    private static final Object lockObj = new Object();

    public static void main(String[] args) {
//        join();
        ThreadMethods.yield();
    }

    private static void join() {
        Thread t = getNormalThread();
        t.start();
        System.out.println(t.getState());
        try {
            t.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t.getState());
    }

    public static void yield() {
        Thread t1 = getSyncThread("线程1");
        t1.start();
        System.out.println(t1.getState());
        ThreadUtils.sleep(100);
        Thread t2 = getSyncThread("线程2");
        t2.start();
        System.out.println(t1.getState());
        System.out.println(t2.getState());
    }

    private static Thread getNormalThread() {
        return new Thread() {
            @Override
            public void run() {
                System.out.println("run");
                ThreadUtils.sleep(1000);
            }
        };
    }

    private static Thread getSyncThread(String name) {
        return new Thread(name) {
            @Override
            public void run() {
                synchronized (lockObj) {
                    System.out.println(Thread.currentThread().getName() + "get the lockObj, start...");
                    ThreadUtils.sleep(1000);
                    System.out.println(Thread.currentThread().getName() + "get the lockObj, end...");
                }
            }
        };
    }
}
